<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/24
 * Time: 22:05
 */

namespace app\index\logic;


use app\common\model\Page;
use think\Db;
use think\Exception;
use think\Log;

class NoteLogic
{
    public function getUserNoteList($user_id,$pagenum=1,$pagesize=20){
        $prefix = config('database.prefix');
        $startno = ($pagenum - 1) * $pagesize;
        $list = model('UserNote')
            ->with('note')
            ->where(array($prefix.'user_note.user_id'=>$user_id))
            ->order('id desc')
            ->limit($startno,$pagesize)
            ->select();
        $total = model('UserNote')
            ->where(array('user_id'=>$user_id))
            ->count();

        foreach ($list as $key=>&$value){
            $temple = model('Temple')
                ->where(array('id'=>$value['note']['temple_id']))
                ->find();
            $value['temple'] = $temple;
            $value['create_time'] = date("Y-m-d", $value['create_time']);
        }
        unset($value);
        $data['list'] = $list;
        $page = new Page($pagenum, $total, $pagesize);
        $page = (array)$page;//转换为数组
        $data['page'] = $page;
        return $data;
    }

    /**
     * 获取寺庙签列表
     * @param $temple_id
     * @param int $pagenum
     * @param int $pagesize
     * @return mixed
     */
    public function getNoteListByTemple($temple_id,$pagenum=1,$pagesize=20){
        $prefix = config('database.prefix');
        $startno = ($pagenum - 1) * $pagesize;
        $list = model('Note')
            ->with('temple')
            ->where(array($prefix.'note.temple_id'=>$temple_id,$prefix.'note.is_delete'=>0))
            ->limit($startno,$pagesize)
            ->select();
        $total = model('Note')
            ->where(array('temple_id'=>$temple_id,'is_delete'=>0))
            ->count();

        foreach ($list as $key=>&$value){
            $value['create_time'] = date("Y-m-d", $value['create_time']);
        }
        unset($value);
        $data['list'] = $list;
        $page = new Page($pagenum, $total, $pagesize);
        $page = (array)$page;//转换为数组
        $data['page'] = $page;
        return $data;
    }

    public function getNoteById($user_note_id){
        $prefix = config('database.prefix');
        $user_note = model('UserNote')
            ->where(array('id'=>$user_note_id))
            ->find();
        $note = model('Note')
            ->with('temple')
            ->where(array($prefix.'note.id'=>$user_note['note_id']))
            ->find();
        $content_list = model('Content')
            ->where(array('note_id'=>$note['id']))
            ->select();
        $note['content_list'] = $content_list;
        $note['user_note'] = $user_note;
        return $note;
    }

    public function createOrUpdateOrder($user_note_id){
        $user_note = model('UserNote')
            ->where(array('id'=>$user_note_id))
            ->find();
        Db::startTrans();
        try{
            $count = Db::name('Order')
                ->where(array('user_note_id'=>$user_note['id']))
                ->count();
            $temple_id = model('Note')
                ->where(array('id'=>$user_note['note_id']))
                ->value('temple_id');
            $money = model('Temple')
                ->where(array('id'=>$temple_id))
                ->value('money');

            if(!$money){
                $money = 1;
            }
            if($count == 0){
                //创建订单
                $order = $this->createOrder($user_note,$money);
            }else{
                $order_number = build_order_no();
                $order = Db::name('Order')
                    ->where(array('user_note_id'=>$user_note['id']))
                    ->find();
                $upload_order['money'] = $money;
                $upload_order['order_number'] = $order_number;
                Db::name('Order')
                    ->where(array('id'=>$order['id']))
                    ->update($upload_order);
                $order['money'] = $money;
                $order['order_number'] = $order_number;
            }
            Db::commit();
            return array('status'=>1,'data'=>$order);
        }catch (Exception $e){
            Db::rollback();
            Log::write('createOrUpdateOrder:'.$e->getMessage());
            return array('status' => 0, 'error' => $e->getMessage());
        }
    }

    private function createOrder($user_note,$money){
        $order['user_id'] = $user_note['user_id'];
        $order['user_note_id'] = $user_note['id'];
        $order['create_time'] = time();
        $order['status'] = 0;
        $order['type'] = 0;

        $order['money'] = $money;
        $order['order_number'] = build_order_no();
        $order = model('Order')->create($order);
        return $order;
    }
}